package src.main.java.com.huawei.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.waf.v1.WafClient;
import com.huaweicloud.sdk.waf.v1.model.*;
import com.huaweicloud.sdk.waf.v1.region.WafRegion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class IgnoreDemo {

    private static final Logger logger = LoggerFactory.getLogger(IgnoreDemo.class);

    public static void main(String[] args) {
        ICredential auth = new BasicCredentials()
                .withAk("<YOUR AK>")
                .withSk("<YOUR SK>");
        WafClient client = WafClient.newBuilder()
                .withCredential(auth)
                .withRegion(WafRegion.valueOf("cn-south-1"))
                .build();
        try {
            createIgnore(client);
        } catch (ConnectionException e) {
            logger.error("A connect timeout exception occurs while the WAF performs some certificate-related operations, exception: {}", e);
        } catch (RequestTimeoutException e) {
            logger.error("A request timeout exception occurs while the WAF performs some certificate-related operations, exception: {}", e);
        } catch (ServiceResponseException e) {
            logger.error("there is service response error, exception: {}", e);
            logger.error("HttpStatusCode: {}", e.getHttpStatusCode());
            logger.error("RequestId: {}", e.getRequestId());
            logger.error("ErrorCode: {}", e.getErrorCode());
            logger.error("ErrorMsg: {}", e.getErrorMsg());
        }
    }

    private static CreateIgnoreRuleResponse createIgnore(WafClient client) {
        // 初始化防护域名或防护网站
        List<String> domains = new ArrayList<>();
        domains.add("www.demo.com");
        // 初始化防护ip
        List<String> contents = new ArrayList<>();
        contents.add("1.1.1.1");
        // 初始化
        CreateCondition createCondition = new CreateCondition()
                .withCategory("url")
                .withLogicOperation("contain")
                .withContents(contents)
                .withIndex(null);
        List<CreateCondition> createConditions = new ArrayList<>();
        createConditions.add(createCondition);
        // 初始化条件列表
        CreateIgnoreRuleRequestBody createIgnoreRuleRequestBody = new CreateIgnoreRuleRequestBody()
                .withDomain(domains)
                .withMode(1)
                .withDescription("demo")
                .withRule("bypass")
                .withConditions(createConditions);
        //初始化防护规则
        CreateIgnoreRuleRequest createIgnoreRuleRequest = new CreateIgnoreRuleRequest()
                .withPolicyId("PolicyId")
                .withBody(createIgnoreRuleRequestBody);
        //创建全局白名单
        CreateIgnoreRuleResponse createIgnoreRuleResponse = client.createIgnoreRule(createIgnoreRuleRequest);
        logger.info(createIgnoreRuleResponse.toString());
        return createIgnoreRuleResponse;
    }
}
